package com.xcjh.utils;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class SystemUtil {
    /**
     * 复制内容
     * @param textToCopy 需要复制到内存的内容
     */
    public static void writeC(String textToCopy){
        StringSelection selection = new StringSelection(textToCopy);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, null);
    }

    /**
     * 粘贴内容
     * @return
     * @throws Exception
     */
    public static String readC() throws Exception {
        Transferable clipboardContent = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        if (clipboardContent != null && clipboardContent.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            return (String) clipboardContent.getTransferData(DataFlavor.stringFlavor);
        }
        return null;

    }
}
